﻿<!-- 正文开始 -->
<style>
    .layui-tree-iconClick .layui-icon-file {
        display: none;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加
                </button>
            </div>
            <!-- 数据表格 -->
            <table id="roleTable" lay-filter="roleTable"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="roleTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs"
       data-dropdown="#roleTbDelDrop{{d.LAY_INDEX}}" no-shade="true">删除</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="auth">权限分配</a>
    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide"
         id="roleTbDelDrop{{d.LAY_INDEX}}"
         style="max-width: 200px;white-space: normal;min-width: auto;margin-left: 10px;">
        <div class="dropdown-anchor"></div>
        <div class="dropdown-popconfirm-title">
            <i class="layui-icon layui-icon-help"></i>
            确定要删除{{d.name}}吗？
        </div>
        <div class="dropdown-popconfirm-btn">
            <a class="layui-btn" btn-cancel>取消</a>
            <a class="layui-btn layui-btn-normal" lay-event="del">确定</a>
        </div>
    </div>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="roleEditDialog">
    <form id="roleEditForm" lay-filter="roleEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">角色名:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入角色名" class="layui-input"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="roleEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<script type="text/html" id="ruleEditState">
    <input type="checkbox" lay-filter="tbStateCk" value="{{d.id}}" lay-skin="switch"
           lay-text="正常|锁定" {{d.status==1?'checked':''}}/>
</script>
<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'tableX', 'tree', 'admin', 'setter', 'dropdown', 'zTree','notice'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var tableX = layui.tableX;
        var tree = layui.tree;
        var admin = layui.admin;
        var setter = layui.setter;
        var notice = layui.notice;

        /* 渲染表格 */
        var insTb = tableX.render({
            elem: '#roleTable',
            url: setter.baseServer + '/role/role_list',
            page: true,
            defaultToolbar: [],
            height: 'full-225',
            cellMinWidth: 100,
            cols: [[
                {field: 'id', type: 'numbers', title: 'id'},
                {field: 'name', title: '名称', width: 200, minWidth: 200},
                {field: 'remark', title: '备注说明'},
                {field: 'status', sort: true, title: '状态', templet: '#ruleEditState', width: 100},
                {title: '操作', toolbar: '#roleTbBar', align: 'center', width: 200, minWidth: 200}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(roleTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(roleTable)', function (obj) {
            if ('edit' === obj.event) { // 修改
                showEditModel(obj.data);
            } else if ('del' === obj.event) { // 删除
                doDel(obj);
            } else if ('auth' === obj.event) {  // 权限管理
                showPermModel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(roleTable)', function (obj) {
            if ('add' === obj.event) { // 添加
                showEditModel();
            } else if ('del' === obj.event) { // 删除
                var checkRows = table.checkStatus('roleTable');
                if (0 === checkRows.data.length) {
                    return notice.msg('请选择要删除的数据', {icon: 2});
                }
                var ids = checkRows.data.map(function (d) {
                    return d.roleId;
                });
                doDel({ids: ids});
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '角色',
                content: $('#roleEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('roleEditForm', mData);
                    // 表单提交事件
                    form.on('submit(roleEditSubmit)', function (data) {
                        notice.msg('加载中...', {icon: 4});
                        admin.req(mData ? '/role/role_edit' : '/role/role_add', JSON.stringify(data.field), function (res) {
                            notice.destroy();
                            if (200 === res.code) {
                                layer.close(dIndex);
                                notice.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                notice.msg(res.msg, {icon: 2});
                            }
                        }, 'post');
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            notice.msg('暂不支持删除',{icon:3})
        }

        /* 权限管理 */
        function showPermModel(obj) {
            var mRoleId = obj.data.id;
            admin.open({
                title: '角色权限分配',
                btn: ['保存', '取消'],
                content: '<form id="rule-check-form" class="layui-form"><div id="rule_check"></div></form>',
                success: function (layero, dIndex) {
                    notice.msg('加载中...', {icon: 4});
                    admin.req('/role/get_role_rule', {role_id: mRoleId}, function (res) {
                        notice.destroy();
                        if (200 === res.code) {
                            tree.render({
                                elem: '#rule_check'
                                , data: res.data
                                , onlyIconControl: true
                                , showCheckbox: true  //是否显示复选框
                                , id: 'checkRules'
                            });
                        } else {
                            notice.msg(res.msg, {icon: 2});
                        }
                    }, 'get');
                    // 超出一定高度滚动
                    $(layero).children('.layui-layer-content').css({'max-height': '300px', 'overflow': 'auto'});
                },
                yes: function (dIndex) {
                    var checkData = tree.getChecked('checkRules');
                    var ids = getChecked_list(checkData);
                    notice.msg('加载中...', {icon: 4});
                    admin.req('/role/role_set_rule', {role_id: mRoleId, rule_ids: ids}, function (res) {
                        notice.destroy();
                        if (200 === res.code) {
                            notice.msg(res.msg, {icon: 1});
                            layer.close(dIndex);
                        } else {
                            notice.msg(res.msg, {icon: 2});
                        }
                    }, 'post');
                }
            });
        }

        /* 修改数据状态 */
        form.on('switch(tbStateCk)', function (obj) {
            notice.msg('加载中...', {icon: 4});
            var data = {};
            data.id = obj.elem.value;
            data.status = obj.elem.checked ? 1 : 0;
            admin.req(setter.baseServer + '/role/role_status', data, function (res) {
                notice.destroy();
                if (200 === res.code) {
                    notice.msg(res.msg, {icon: 1});
                } else {
                    notice.msg(res.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox', $('#eDialogTable').next().attr('lay-filter'));
                }
            }, 'post');
        });

        // 获取选中权限节点的id
        function getChecked_list(data) {
            var id = "";
            $.each(data, function (index, item) {
                if (id != "") {
                    id = id + "," + item.id;
                } else {
                    id = item.id;
                }
                var i = getChecked_list(item.children);
                if (i != "") {
                    id = id + "," + i;
                }
            });
            return id;
        }
    });
</script>
